" python.py" file output

" python.tcl" file input

"  " string akku

{} list src

{} list bodysrc

02 variable indentcnt

: indent ( --  )
       " \n" akku append
       indentcnt 0 do "   " akku append loop

"                            ..\n" string indentchars

"" string pushval

code pushval_rest ( -- )
       set ::pushval [string range "$::pushval" 1 end]

: append_push ( -- )
       pushval get .
       0 pushval index " \$" = if pushval_rest then
       " ppush( " akku append
       pushval get akku append
       "  )" akku append

: tr-body ( | cv args -- )
       \ "" akku ! \ only debug!!
       1  indentcnt !
       1 args !
       indent
       bodysrc length 0 do
               doi bodysrc cv !
               cv @ .
               cv " set" = if
                       1 doi + bodysrc akku append "  = ppop()" akku append
                       3
               else    cv " ;" = if
                       indent
                       1
               else cv " INDENT" = if
                       indentcnt incr
                       1
               else cv " UNINDENT" = if
                       -1 indentcnt add
                       1 \ skip ; too
               else cv " push" = if
                       doi 1+ bodysrc pushval ! append_push
                       2
               else cv " ---" = if
                       0 args !
                       1
               else cv " +++" = if
                       1 args !
                       1
               else
                       cv akku append
                       args if " ()" else "  " then akku append
                       1
               then then then then then then then

       +loop
       " \n" akku append
       ." --"
       akku .

: translate ( | mode -- )
       "" akku !
       src length 0 do
               0 doi + src mode ! mode .
               mode " proc" = if
                       " \ndef " akku append
                       1 doi + src akku append
                       " ( " akku append
                       2 doi + src @ akku append
                       " ) : \n" akku append
                       3 doi + src @ bodysrc setlist
                       tr-body
                       4
                  \ ." type: " 0 src .
                  \ ." name: " 1 src . cr
                  \ ." args: " 2 src . cr
                  \ ." body: " 3 src . cr
               else mode " ;" = if
                       " \n" akku append
                       1
               else
                       mode akku append
                       1
               then    then
       +loop

: process ( -- )
       ." ---" !s
       output open-w
       input open-r
       "  " akku set
       begin input eof not while
               input get
               dup akku append " \n" akku append
       repeat
       akku get .
       akku get src setlist
       translate
       akku get output put
       input close output close

